package org.jeecg.modules.demo.mes.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.demo.mes.entity.MesTechnology;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedure;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedureCheckBom;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedureMaterialBom;
import org.jeecg.modules.demo.mes.mapper.MesTechnologyMapper;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureCheckBomService;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureMaterialBomService;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureService;
import org.jeecg.modules.demo.mes.service.IMesTechnologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description: 工艺表
 * @Author: jeecg-boot
 * @Date: 2022-10-31
 * @Version: V1.0
 */
@Service
public class MesTechnologyServiceImpl extends ServiceImpl<MesTechnologyMapper, MesTechnology> implements IMesTechnologyService {

    @Autowired
    private IMesTechnologyProcedureService mesTechnologyProcedureService;


    /**
     * 工艺表通用单表查询
     *
     * @param cxMap 查询map
     * @return list
     * @throws JeecgBootException
     */
    @Override
    public List<MesTechnology> queryTechnologyListSelective(Map<String, Object> cxMap) throws JeecgBootException {
        if (cxMap == null) {
            return new ArrayList<>();
        }
        return super.baseMapper.queryTechnologyListSelective(cxMap);
    }

    /**
     * 修改工艺表状态
     *
     * @param id               主键id
     * @param technologyStatus 状态
     * @throws JeecgBootException
     */
    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void updateStatusById(String id, String technologyStatus, String loginName) throws JeecgBootException {
        if (StringUtils.isBlank(id)) {
            throw new JeecgBootException("唯一id为空");
        }
        if (StringUtils.isBlank(technologyStatus) || !Arrays.asList("1", "2").contains(technologyStatus)) {
            throw new JeecgBootException("非法的工艺状态");
        }
        final Map<String, Object> technology = new HashMap<>();
        technology.put("id", id);
        technology.put("technologyStatus", technologyStatus);
        technology.put("updateBy", loginName);
        technology.put("updateTime", new Date());
        if (StringUtils.equals("1", technologyStatus)) {
            //启用--》添加启用时间
            technology.put("technologyEnableTime", new Date());
        } else {
            //禁用
            technology.put("technologyEnableTimeIsNull", true);
        }
        super.baseMapper.updateStatusById(technology);
    }

    /**
     * 删除工艺（删除所有和工艺相关的表）
     *
     * @param id id
     * @throws JeecgBootException
     */
    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void removeTechnologyById(String id) throws JeecgBootException {
        //删除工艺表
        this.removeById(id);
        //删除工序表
        final List<MesTechnologyProcedure> mesTechnologyProcedures = mesTechnologyProcedureService.queryTechnologyProcedureListSelective(new HashMap<String, Object>() {{
            put("technologyId", id);
        }});
        if (mesTechnologyProcedures == null) {
            return;
        }
        for (MesTechnologyProcedure mesTechnologyProcedure : mesTechnologyProcedures) {
            final String mesTechnologyProcedureId = mesTechnologyProcedure.getId();
            mesTechnologyProcedureService.removeTechnologyProcedureById(mesTechnologyProcedureId);
        }

    }
}
